import '../style/login.less';
import $ from 'jquery';
import './common';
import './plugins/MD5';

$(function() {
    var $form = $("form");

    $form.on("submit", function(e) {
        const formdata = $form.serializeArray().reduce((prev, item) => {
            prev[item.name] = item.value;
            return prev;
        }, {});
        if (!formdata.username) {
            $(".num-err").removeClass("hide");
            $(".num-err").find("em").text("请输入账号");
            $("input[name='username']").focus();
        } else if (!formdata.password) {
            $(".pass-err").removeClass("hide");
            $(".pass-err").find("em").text("请输入密码");
            $("input[name='password']").focus();
        } else {
            let salt = $.randomString();
            formdata["salt"] = salt
            formdata["password"] = $.cryptSaltPsw(salt, formdata["password"]);
            $.ajax({
                url: "portal/login.do",
                type: "POST",
                data: formdata,
                success: function(res) {
                    if (res.success) {
                        window.location.href = "/";
                    } else {
                        $(".tel-warn").removeClass("hide");
                        $(".tel-warn").find("em").text("账号密码错误");
                    }
                }
            });
        }
        return false;
    });

    var oInput = $(".form-data input");

    oInput.focus(function() {
        $(this).siblings("label").hide();
    });
    oInput.blur(function() {
        if ($(this).val() == "") {
            $(this).siblings("label").show();
        }
        $(this).next().addClass('hide');
    });

});
